package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;


@Controller
@RequestMapping("registerController")
public class RegisterController {
    @Reference
    private SellerService sellerService;

    //跳转注册
    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    //跳转登录
    @RequestMapping("toLogin")
    public String toLogin() {
        return "shoplogin";
    }

    //跳转入驻协议
    @RequestMapping("toSampling")
    public String toSampling() {
        return "sampling";
    }

    //注册
    @RequestMapping("registerInfo")
    @ResponseBody
    public StatusCode rejisterInfo(TbSeller tbSeller) {
        String salt = ShiroUtils.generateSalt(6);
        tbSeller.setSalt(salt);
        String md5 = ShiroUtils.encryptPassword("MD5", tbSeller.getPassword(), salt, 3);
        tbSeller.setPassword(md5);
        Boolean b = sellerService.insertSeller(tbSeller);
        if (!b) {
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //登录
    @RequestMapping("loginInfo")
    @ResponseBody
    public StatusCode loginInfo(String sellerId, String password) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (sellerId == null && password == null) {
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.封装token
        UsernamePasswordToken token = new UsernamePasswordToken(sellerId, password);
        //3.调用登陆方法
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            map.put("messg", "用户名不存在");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        } catch (IncorrectCredentialsException e) {
            map.put("messg", "密码错误");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        } catch (LoginException e) {
            map.put("messg", "店铺未审核");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        map.put("messg", "登陆成功");
        return StatusCode.build(ConstantEnum.SUCCESS).setT(map);
    }

    //跳转主页
    @RequestMapping("toIndex")
    public String toIndex() {
        return "admin/index";
    }

    //跳转home
    @RequestMapping("toHome")
    public String toHome(){
        return "admin/home";
    }
}
